//
//  LYAcademicCertificateListViewController.m
//  Btpin
//
//  Created by liuyan on 2019/4/18.
//  Copyright © 2019年 Vic. All rights reserved.
//

#import "LYAcademicCertificateListViewController.h"
#import "LYCertificateListCell.h"

#import "PMineResumeEducationViewController.h"
#import "PMineResumeWorkViewController.h"


@interface LYAcademicCertificateListViewController ()<UITableViewDelegate, UITableViewDataSource, PMineResumeEducationViewControllerDelegate, PMineResumeWorkViewControllerDelegate>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LYAcademicCertificateListViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubview];
    if (self.state == CertificateListStateAcademic) {
        self.title = @"学历认证";
    }else{
        self.title = @"职位认证";
    }
    [self requestData];
}

- (void)createSubview{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-HeightOfStatusBarAndNavigationBar) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = RGB(247, 247, 247);
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    
    [self.tableview registerClass:[LYCertificateListCell class] forCellReuseIdentifier:kCellIdentifier_LYCertificateListCell];
    [self.view addSubview:self.tableview];
    
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    headerview.backgroundColor = [UIColor whiteColor];
    self.tableview.tableHeaderView = headerview;
    
    UILabel *messagelabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, WIDTH -30, 30)];
    messagelabel.font = [UIFont systemFontOfSize:14];
    messagelabel.adjustsFontSizeToFitWidth = YES;
    messagelabel.textColor = [UIColor orangeColor];
    if (self.state == CertificateListStateAcademic) {
       messagelabel.text = @"完成以下学历认证，提升信用分数";
    }else{
       messagelabel.text = @"完成以下工作经验认证，提升信用分数";
    }
    [headerview addSubview:messagelabel];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYCertificateListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYCertificateListCell forIndexPath:indexPath];
    if (self.state == CertificateListStateAcademic) {
        cell.educatModel =  self.dataArray[indexPath.row];
    }else{
        cell.workModel = self.dataArray[indexPath.row];
    }
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.state == CertificateListStateAcademic) {
        //学历认证
        PMineResumeEducationViewController *PMREVC = [[PMineResumeEducationViewController alloc] init];
        PMREVC.delegate = self;
        PMineResumeEducationInfoModel *model = self.dataArray[indexPath.row];
        PMREVC.type = AddOrEditTypeOfEdit;
            PMREVC.model = model;
        [self.navigationController pushViewController:PMREVC animated:YES];
    }else{
        //职位认证
            PMineResumeWorkViewController *PMRWVC = [[PMineResumeWorkViewController alloc] init];
            PMRWVC.delegate = self;
            PMineResumeWorkInfoModel *model = self.dataArray[indexPath.row];
            PMRWVC.type = AddOrEditTypeOfEdit;
            PMRWVC.model = model;
            [self.navigationController pushViewController:PMRWVC animated:YES];
        }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

#pragma mark -- loadData
- (void)requestData{
     SVPHUDShow;
    NSString *url;
    if (self.state == CertificateListStateAcademic) {
        url = PersonMine_GetEduInfo_URL;
    }else{
        url = PersonMine_GetWorkInfo_URL;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"info_id"] = nil;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:url params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDDismiss;
            [weakSelf organizeData:responseObject[@"data"]];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

- (void)organizeData:(NSArray *)dataArray{
    self.dataArray = [NSMutableArray array];
     if (self.state == CertificateListStateAcademic) {
         for (NSDictionary *dic in dataArray) {
             PMineResumeEducationInfoModel *model = [[PMineResumeEducationInfoModel alloc] init];
             [model setValuesForKeysWithDictionary:dic];
             [self.dataArray addObject:model];
         }
     }else{
         for (NSDictionary *dic in dataArray) {
             PMineResumeWorkInfoModel *model = [[PMineResumeWorkInfoModel alloc] init];
             [model setValuesForKeysWithDictionary:dic];
             [self.dataArray addObject:model];
         }
     }
    [self.tableview reloadData];
}

#pragma mark -- delegate
- (void)pMineResumeEducationViewControllerBackRefresh:(BOOL)refresh{
    //教育刷新
    [self requestData];
}

- (void)pMineResumeWorkViewControllerBackRefresh:(BOOL)refresh{
    //工作经验
    [self requestData];
}

@end
